package com.ybb.production.controller;

import com.ybb.production.domain.GarmentProductionProcess;
import com.ybb.production.domain.GarmentProductionProcessTemp;
import com.ybb.production.dto.GarmentProductionProcessTempDTO;
import com.ybb.production.service.IGarmentProductionProcessService;
import com.ybb.production.service.IGarmentProductionProcessTempService;
import com.yubb.common.annotation.Log;
import com.yubb.common.core.controller.BaseController;
import com.yubb.common.core.domain.AjaxResult;
import com.yubb.common.core.domain.R;
import com.yubb.common.core.page.TableDataInfo;
import com.yubb.common.enums.BusinessType;
import com.yubb.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 生产工序Controller
 *
 * @author ruoyi
 * @date 2023-09-08
 */
@RestController
@RequestMapping("/production/process")
@Api(tags = "生产工序")
public class GarmentProductionProcessController extends BaseController {
    @Autowired
    private IGarmentProductionProcessService garmentProductionProcessService;

    @Autowired
    private IGarmentProductionProcessTempService garmentProductionProcessTempService;

    @RequiresPermissions("production:process:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询生产工序列表")
    public TableDataInfo<GarmentProductionProcess> list(GarmentProductionProcess garmentProductionProcess) {
        startPage();
        List<GarmentProductionProcess> list = garmentProductionProcessService.selectGarmentProductionProcessList(garmentProductionProcess);
        return getDataTable(list);
    }

    @RequiresPermissions("production:process:edit")
    @Log(title = "生产工序", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "批量修改工序")
    public AjaxResult edit(@RequestBody List<GarmentProductionProcess> productionProcesses) {
        return toAjax(garmentProductionProcessService.updateGarmentProductionProcess(productionProcesses));
    }

    @RequiresPermissions("production:process:remove")
    @Log(title = "生产工序", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除工序")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(garmentProductionProcessService.deleteGarmentProductionProcessByIds(ids));
    }

    @Log(title = "生产工序", businessType = BusinessType.IMPORT)
    @RequiresPermissions("production:process:import")
    @ApiOperation(value = "导入工序")
    @PostMapping("/importProcessData")
    public R<List<GarmentProductionProcess>> importProcessData(MultipartFile file) throws Exception {
        ExcelUtil<GarmentProductionProcess> util = new ExcelUtil<>(GarmentProductionProcess.class);
        List<GarmentProductionProcess> userList = util.importExcel(file.getInputStream());
        return R.ok(userList);
    }


    @RequiresPermissions("production:process:edit")
    @Log(title = "工序模板-保存工序模板", businessType = BusinessType.UPDATE)
    @PostMapping("/addProcessTemp")
    @ApiOperation(value = "工序模板-保存工序模板")
    public AjaxResult addProcessTemp(@RequestBody GarmentProductionProcessTempDTO tempDTO) {
        return toAjax(garmentProductionProcessTempService.saveTemp(tempDTO));
    }

    @RequiresPermissions("production:process:list")
    @GetMapping("/tempList")
    @ApiOperation(value = "工序模板-列表")
    public List<GarmentProductionProcessTemp> tempList() {
        return garmentProductionProcessTempService.selectGarmentProductionProcessTempList(new GarmentProductionProcessTemp());
    }

    @RequiresPermissions("production:process:remove")
    @Log(title = "工序模板-删除模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/temp/{ids}")
    @ApiOperation(value = "工序模板-删除模板")
    public AjaxResult tempRemove(@PathVariable Long[] ids) {
        return toAjax(garmentProductionProcessTempService.deleteGarmentProductionProcessTempByIds(ids));
    }


    @RequiresPermissions("production:process:list")
    @GetMapping("/tempDetail/{id}")
    @ApiOperation(value = "工序模板-模板内容")
    public List<GarmentProductionProcess> tempDetail(@PathVariable Long id) {
        return garmentProductionProcessTempService.tempDetail(id);
    }

}
